from decimal import Decimal, ROUND_HALF_UP

def convert_to_wan(amount_in_yuan, divisor=10000, decimal_places=2):
    """  
    将单位为元的数据转换为万元，并可以自定义保留的小数位数。备用名：yuan2w
  
    :param amount_in_yuan: 原始金额，单位为元，可以是任意类型的数据 
    :param decimal_places: 转换后保留的小数位数，默认为2  
    :return: 转换后的金额，单位为万元，类型为Decimal  
    """
    amount_in_yuan = amount_in_yuan.replace(' ', '').replace(',', '')
    amount_type = type(amount_in_yuan)
    if amount_type == str:
        try:
            amount_in_yuan = eval(amount_in_yuan)
        except Exception as e:
            return amount_in_yuan
    elif amount_type == type(None):
        return 0
    amount_type = type(amount_in_yuan)
    if amount_type not in (int, float, Decimal):  # 如果输入值类型不是整数、浮点数或Decimal
        print(f'输入值 {amount_in_yuan} ({amount_type}) 类型不正确，请检查输入值')
        return
    amount = Decimal(str(amount_in_yuan))      # 将输入转换为Decimal类型
    wan_yuan = amount / Decimal(str(divisor))     # 转换为万元
    if decimal_places == 1:
        quantized_wan_yuan = wan_yuan.quantize(Decimal('1'), rounding=ROUND_HALF_UP)
    else:
        quantized_wan_yuan = wan_yuan.quantize(Decimal(f'1.{"0" * (decimal_places - 1)}1'), rounding=ROUND_HALF_UP)      # 使用quantize方法进行四舍五入，并保留指定的小数位数  
    return quantized_wan_yuan 
